<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2024/6/1
 * Time: 10:36
 */

namespace app\controller\index;
use app\model\index\User AS model;
use app\model\index\UserSearchKeyword;

class User extends Base
{
    public function find(){

        return $this->view('/index/user/find',['info' => $this->user]);
    }

    public function info(){
        $post = input('post.');
        if (!empty($post))
            return $this->msg($this->user);
        $list = model::where('pid',$this->user->id)->select();
        return $this->view('/index/user/info',['info' => $this->user,'list' => $list]);
    }

    public function list(){
        $where = [];
        $post = input('post.');
        !empty($post['status']) && $where[] = ['status','=',$post['status']];
        !empty($post['username']) && $where[] = ['username','like','%'.$post['username'].'%'];
        $list = model::where('pid',$this->user->id)
            ->where($where)
            ->order('id DESC')
            ->paginate(['page' => $this->page,'list_rows' => $this->size]);
        return $this->msg($list);
    }

    public function save(){
        $username = input('username');
        if (empty($username))
            return $this->fail('账号是必填项');
        $password = input('password');
        if (empty($password))
            return $this->fail('密码是必填项');
        $count = model::where('username',$username)->count();
        $nickname = input('nickname','');
        $company_name = input('company_name','');
        $mobile = input('mobile');
        if ($count > 0)
            return $this->fail('账号已经存在,请换一个');
        $info = model::create([
            'username' => $username,
            'password' => $password,
            'realname' => $nickname,
            'company_name' => $company_name,
            'mobile' => $mobile,
            'pid' => $this->user->id,
            'is_son' => input('user_son_type',1),
            'amount' => 0,
            'price_ratio' => $this->user->price_ratio,
            'status' => 1
        ]);
        return $this->msg($info);
    }

    public function status($id,$status){
        model::find($id)->save(['status' => $status]);
        return $this->msg();
    }

    public function keywords(){
        $keywords = UserSearchKeyword::where('username',$this->user->username)
            ->with(['keywords'])
            ->paginate([
                'page' => $this->page,
                'list_rows' => $this->size
            ]);
        return $this->msg($keywords);
    }

    public function password(){
        $password = input('password');
        if (empty($password))
            return $this->view('/index/user/password');
        if ($this->user->password == $password)
            return $this->fail('新密码不能和旧密码相同');
        $this->user->password = $password;
        $this->user->save();
        return $this->msg();
    }

    public function del_keyword(){
        $keyword = input('keyword');
        UserSearchKeyword::where('keyword',$keyword)
            ->where('username',$this->user->username)
            ->delete();
        return $this->msg();
    }

    public function set_status(){
        $username = input('username');
        $find = model::where('username',$username)->find();
        if ($find->status == 1){
            $find->status = 2;
        }else{
            $find->status = 1;
        }
        $find->save();
        return $this->msg($find);
    }
}